package com.ctshk.rpc.order.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.*;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.rpc.order.goods.entity.Order;
import com.ctshk.rpc.order.goods.mapper.OrderMapper;
import com.ctshk.rpc.order.goods.service.IOrderTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

/**
 * TODO
 * 中旅到家商品订单表  服务实现类
 *
 * @author huangxiansheng
 * @date 2021/5/21 2:30 下午
 */
@Slf4j
@DubboService
public class OrderTaskServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderTaskService {

    @Autowired
    OrderMapper orderMapper;

    @Override
    public Result notPaid(Long id) {
        Order order = orderMapper.selectOne(new QueryWrapper<Order>().lambda()
                .eq(Order::getId, id)
                .eq(Order::getPaymentStatus, OrderPaymentStatus.UN_PAYMENT)
                .eq(Order::getScheduledStatus,OrderScheduledStatus.CONFIRM_RESERVE.getCode())
                .eq(Order::getIsDeleted, IsDeletedCode.NO.getCode()));
        if (order == null) {
            log.error("【商品订单定时任务-未支付超时】找不到订单,id:{}", id);
            throw new BusinessException(SystemError.MALL_MERCHANDISE_23033);
        }
        order.setPaymentStatus(OrderPaymentStatus.ALREADY_CANCEL.getCode());
        order.setScheduledStatus(OrderScheduledStatus.RESERVE_FAIL.getCode());
        order.setCancelStatus(GoodsApprovalStatusConstants.TO_BE_SUBMITTED.getCode());
        order.setGmtModified(LocalDateTime.now());
        orderMapper.updateById(order);
        return Result.success();
    }
}
